<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">按钮</button>
    <script>
      const oBtn = document.getElementById("btn");

      //这个是我们事件的核心代码
      function center(e) {
        console.log("我是点击事件的核心代码!!!", this, e);
      }

      function throttle(fn, time) {
        //throttle内部返回的函数才是真正的事件函数,这个函数的e和this才是正确的
        let lastTime = 0;
        return function (e) {
          //看门狗的过程
          const now = Date.now();
          if (now - lastTime < time) {
            return;
          }
          lastTime = now;
          //执行事件的核心代码
          fn.call(this, e);
        };
      }

      //节流函数的用法
      oBtn.onclick = throttle(center, 1000);
    </script>
  </body>
</html>
